package com.fingard.dsp.task.command.receipt;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;

import com.fingard.FGBiz;
import com.fingard.client.DspClient;
import com.fingard.common.utils.json.JSONUtils;
import com.fingard.constant.Format;
import com.fingard.diagnostics.LogHelper;
import com.fingard.dsp.bank.dbobj.receipt.ReceiptData;
import com.fingard.dsp.bank.dbobj.receipt.ReceiptDataHelper;
import com.fingard.dsp.bank.directConfig.DataPushSet;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.dsp.bank.helpers.dbhelpers.SerializeHelper;
import com.fingard.dsp.bank.sepro.ReceiptPro;
import com.fingard.dsp.task.DelayCounter;
import com.fingard.dsp.task.ITask;
import com.fingard.dsp.task.command.SyncReceipt;
import com.fingard.net.FormSubmit;
import com.fingard.net.TcpReceiver;
import com.fingard.net.WebRequest;
import com.fingard.text.StringHelper;
import com.fingard.util.DateHelper;
import com.fingard.util.TransIDHelper;
import com.fingard.xml.XmlTextReader;

/**推送回单数据*/
public class PushReceiptData extends SyncReceipt implements ITask {

	//数据推送delayCounter
	private static DelayCounter delayCounterPush = new DelayCounter(100, 2);//最大延时100分钟，按平方计算，即4次=4*4=16分钟
	
	public static void resetDelayCounter(){
		delayCounterPush.resetZeroCount();
	}
	
	public PushReceiptData(){
		logCode = "task"+File.separator+"receipt"+File.separator+"PushReceiptData";
	}
	
	public PushReceiptData(String pLogCode){
		logCode = pLogCode;
	}
	
	private static long polledCount = 0;//轮询次数
	public long getPolledCount(){
		return polledCount;
	}
	
	private static Date lastPolledTime;//最近轮询时间
	public Date getLastPolledTime(){
		return lastPolledTime;
	}
	
	/**pParams的key已转小写
	 *          <paramList>
                    <bank></bank>
                    <beginDayCount>30</beginDayCount>
                    <endDayCount>0</endDayCount>
                </paramList>
           */
	@Override
	public void execTask(HashMap<String, String> pParams) {
		try{
			lastPolledTime = new Date();
			boolean isDisposable = false;//当前是否一次性打印，即第一次需要打印
			long tmpDisposableCount = polledCount;
			polledCount++;
			if(tmpDisposableCount == 0){
				isDisposable = true;
			}
			
			proConfig = ReceiptPro.getReceiptProConfig();
			
			int[] days = getBeginEndDay(pParams);
			int beginDayCount = days[0];
			int endDayCount = days[1];
    		
    		//数据推送
    		if(proConfig.pushSetList.size()>0){
    			if(polledCount % 20 == 1){
    				FGBiz.logTrace.debug(logCode,logID + "\t开始执行回单数据同步"+polledCount);
    			}
    			if(isDisposable){
    				StringBuilder sbLog2 = new StringBuilder();
					for(DataPushSet eachPushSet : proConfig.pushSetList){
						sbLog2.append("id:"+eachPushSet.id+",pushType:"+eachPushSet.pushType+",server:"+eachPushSet.pushServerUrl+"\r\n");
					}
					FGBiz.logTrace.info(logCode,sbLog2.toString());
					FGBiz.logTrace.info(logCode, "分段笔数："+proConfig.getPushStepLen());
    			}
    			execPush(beginDayCount, endDayCount);
    			if(polledCount % 20 == 1){
    				FGBiz.logTrace.info(logCode, logID + "\t本次数据推送运行结束"+polledCount);
    			}
    		}else{
    			if(isDisposable){
					FGBiz.logTrace.info(logCode,logID + "\t无开启的dataPushSet配置节点");
				}
    		}
    		
		}catch(Throwable t){
			FGBiz.logTrace.exception(logCode, t);
		}finally{
			
		}
	}

	/**执行数据推送*/
	private void execPush(int pBeginDayCount, int pEndDayCount){
		if(delayCounterPush.isDelay()){
			return;
		}
		int totalPushUp = 0;
		if(proConfig.pushSetList.size()>0){
			Date nowDay = new Date();
			int retryMins = proConfig.getValueAsInt("retryPushMins", 30, "");
			ArrayList<String> tenantList = FGBiz.dspCenter.getDistinctTenant();
			for(String eachTenant : tenantList){//遍历所有租户
				for(int dCount=pBeginDayCount; dCount<=pEndDayCount; dCount++){
	    			Date eachDay = DateHelper.addDays(nowDay, dCount);
	    			ReceiptDataHelper receiptHelper = null;
	    			try{
	    				receiptHelper = new ReceiptDataHelper(eachTenant, eachDay);
	            		int maxCount = ReceiptDataHelper.getMaxTabCount();
	            		for(int idx=0;idx<maxCount;idx++){
	            			String logPre = logID +"\t"+ Format.Date10Format.format(eachDay)+"表"+idx;
	            			ArrayList<Long> uridList = receiptHelper.getNeedPushList(idx, eachDay,retryMins);
	            			if(uridList != null && uridList.size() > 0){
	            				totalPushUp += uridList.size();
	            				FGBiz.logTrace.info(logCode,logPre+"，需推送"+uridList.size()+"条");
	            				//执行上传
	            				try{
	            					int succCount = pushData(receiptHelper, uridList, idx, eachDay);
	            					FGBiz.logTrace.info(logCode,logPre+"，成功推送"+succCount+"条");
	            				}catch(Exception e){
	            					FGBiz.logTrace.exception(logCode,e,logPre);
	            				}
	            			}else{
	            				int rdCount = receiptHelper.getPushedCount(idx, eachDay);
	            				int c2 = receiptHelper.getDownedCount(idx, eachDay);
	            				int c3 = receiptHelper.getTotalCount(idx, eachDay);
	            				int c4 = receiptHelper.getUpedCount(idx, eachDay);
	            				if(rdCount>0||c2>0||c3>0||c4>0){
	            					FGBiz.logTrace.info(logCode, logPre+"，无需要推送的数据，已推送成功"+rdCount+"条，已上传"+c4+"条，已下载"+c2+"条，总数"+c3+"条");
	            				}
	            			}
	            		}
	    			}catch(Exception ex){
	    				FGBiz.logTrace.exception(logCode, ex);
	    			}finally{
	    				if(receiptHelper!=null){
	    					receiptHelper.close();
	    				}
	    			}
	    		}
			}
		}
		if(totalPushUp == 0){
			delayCounterPush.increaseZeroCount();
			FGBiz.logTrace.info(logCode, logID + "\t需推送条数为0，增加轮询延时"+delayCounterPush.getDelayMinutes()+"分钟");
		}else{
			delayCounterPush.resetZeroCount();
		}
	}
	
	/**推送数据
	 * @throws ParseException 
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 * @throws UnsupportedEncodingException */
	private int pushData(ReceiptDataHelper pHelper, ArrayList<Long> pList, int pIdx, Date pBusDate) throws IllegalArgumentException, IllegalAccessException, SQLException, ParseException, UnsupportedEncodingException {
		int sCount = 0;
		ArrayList<Long> rpUridList = null;//每100条一推送
		int stepLen = proConfig.getPushStepLen();//分段最大笔数
		DspClient dspClient = null;
		try{
			for(int idIdx=0;idIdx<pList.size();idIdx++){
				long urid = pList.get(idIdx);
				//ReceiptData pData = pHelper.loadFromDbByUrid(pIdx, urid);
				//if(pData == null){
				//	FGBiz.logTrace.error(logCode,"tabidx:"+pIdx+",urid:"+urid+",loadFromDb:null");
				//	continue;
				//}
				//pData.pushTime = new Date();
				if(rpUridList == null){
					rpUridList = new ArrayList<Long>();
				}
				rpUridList.add(urid);
				if(rpUridList.size() == stepLen || idIdx==(pList.size()-1)){//到分段最大笔数，或最后一笔了
					ArrayList<ReceiptData> rDataList = pHelper.loadFromDbByUridList(pIdx, rpUridList);
					if(rDataList.size()<=0){
						FGBiz.logTrace.error(logCode, "tabidx:"+pIdx+",urid:"+StringHelper.joinLongArray(",", rpUridList)+",loadFromDb.size:"+rDataList.size());
						continue;
					}
					//从数据库查出后，实际的uridlist
					ArrayList<Long> factUridList = new ArrayList<Long>();
					for(int rIdx=0;rIdx<rDataList.size();rIdx++){
						ReceiptData aData = rDataList.get(rIdx);
						factUridList.add(aData.urid);
					}
					Date pushTime = new Date();
					String pushInfo="";
					int upSuccCount = 0;
					int upFailCount = 0;
					try{
						for(DataPushSet eachPushSet : proConfig.pushSetList){
							String[] data ;
							if("nbGateway".equalsIgnoreCase(eachPushSet.pushType)) {
								StringBuilder sendJson = buildJosnSb2(pHelper.getTenant(),rDataList);
								FGBiz.logTrace.debug(logCode,logID + "\t推送的数据："+sendJson.toString());
								data = pushToNbGateway(eachPushSet.pushServerUrl.replaceAll("\\\\","/"), sendJson);
		                    }else if("ats".equalsIgnoreCase(eachPushSet.pushType)) {
		                    	StringBuilder sendJson = buildJosnSb(pHelper.getTenant(),rDataList);
		                    	FGBiz.logTrace.debug(logCode,logID + "\t推送的数据："+sendJson.toString());
		                        data = sendToATS(eachPushSet.pushServerUrl, sendJson.toString(), eachPushSet.getCharset("gbk"), eachPushSet.getReadTimeout(60000));
		                    }else if("remoteHost".equalsIgnoreCase(eachPushSet.pushType)) {
		                    	StringBuilder sendXml = buildXmlSb(pHelper.getTenant(), rDataList, pBusDate);
		                    	FGBiz.logTrace.debug(logCode,logID + "\t发送的报文："+sendXml.toString());
		                    	if(dspClient == null){
									dspClient = new DspClient(eachPushSet.pushServerUrl);
									dspClient.timeoutSec = eachPushSet.getReadTimeoutSec(60000);
									dspClient.setCharset(eachPushSet.getCharset("gbk"));
								}
		                        data = pushToRemoteDsp(dspClient, sendXml);
		                    }else if("atsInsu".equalsIgnoreCase(eachPushSet.pushType)){
		                    	data = pushToAtsInsu(eachPushSet, rDataList);
		                    }else if("atsCheck".equalsIgnoreCase(eachPushSet.pushType)){
		                    	data = sendNotifyToAtsCheck(eachPushSet, rDataList);
		                    }else{
		                    	data =  new String[]{"",""};
		                    	data[1] = "不支持的推送方式：" + eachPushSet.pushType;
		                    }
							pushInfo = data[1];
							if (data[0].length() == 0 && "ok".equalsIgnoreCase(data[1])) {
	                        	upSuccCount++;
	                        	FGBiz.logTrace.debug(logCode, logID + "\t5."+eachPushSet.pushType+"推送数据成功" + rDataList.size() + "笔");
	                        }else if (data[0].length() == 0 && "ing".equalsIgnoreCase(data[1])) {
	                        	FGBiz.logTrace.debug(logCode, logID + "\t5."+eachPushSet.pushType+"提交数据" + rDataList.size() + "笔，等待结果通知");
	                        }else {
	                        	upFailCount++;
	                        	FGBiz.logTrace.error(logCode, logID + "\t5."+eachPushSet.pushType+"推送数据失败" + rDataList.size() + "笔："+data[1]);
	                        }
						}
					}catch(Exception ex){
						pushInfo = LogHelper.getStackTrace(ex);
						FGBiz.logTrace.exception(logCode, ex);
					}
					
					if(upSuccCount == proConfig.pushSetList.size()){
						//更新数据库
						sCount += rDataList.size();
						int rowsUpd = pHelper.updatePushSuccess(pIdx, factUridList, pushTime, pushInfo);
						if(rowsUpd == factUridList.size()){
							FGBiz.logTrace.info(logCode, "推送成功更新完成："+rowsUpd+"笔");
						}else{
							FGBiz.logTrace.error(logCode, "urid:" + StringHelper.joinLongArray(",", factUridList) + ",更新推送成功影响行数："+rowsUpd+"与预期值"+factUridList.size()+"不符");
						}
					}else{
						String pushState = "";
						if(upFailCount == 0){
							pushState = "4";
							pushInfo = "已提交，等结果通知";
						}else{
							pushState = "3";
							FGBiz.logTrace.info(logCode, "urid:" + StringHelper.joinLongArray(",", factUridList) + ", 需推送"+proConfig.pushSetList.size()+"个服务器，成功推送"+upSuccCount+"个，推送信息："+pushInfo);
						}
						for(ReceiptData fData:rDataList){//更新失败，逐笔更新，为控制并发
							fData.pushTime = pushTime;
							fData.pushState = pushState;
							fData.pushInfo = pushInfo;
							int rowsAff = pHelper.updatePushState(pIdx, fData);
							if(rowsAff != 1){
								FGBiz.logTrace.info(logCode, "urid:" + fData.urid + ", receiptNo:" + fData.receiptNo + ", 推送状态"+fData.pushState+"，更新影响行数：" + rowsAff);
							}
						}
					}
					
					rpUridList = null;//重置，准备下一组
				}
			}
		}catch(Exception ex){
			FGBiz.logTrace.exception(logCode, ex);
		}finally{
			dspClient = null;
		}
		
		return sCount;
	}
	
	/**组装xml格式
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException */
	private StringBuilder buildXmlSb(String pTenant, ArrayList<ReceiptData> pRpDataList, Date pBusDate) throws ParseException, IllegalArgumentException, IllegalAccessException{
		//不需要转xml的字段，小写
		HashSet<String> ignoreSet = new HashSet<String>();
		ignoreSet.add("pushstate");
		ignoreSet.add("pushtime");
		ignoreSet.add("pushinfo");
		ignoreSet.add("createdon");
		ignoreSet.add("lastmodifiedon");
		ignoreSet.add("rowversion");
		StringBuilder sbXml = new StringBuilder();
		sbXml.append("<ATSYH><TransReq>\r\n<DataType>receiptPro</DataType><TransParam>\r\n");
		sbXml.append("<Tenant>"+pTenant+"</Tenant>\r\n");
		sbXml.append("<BusDate>"+Format.Date10Format.format(pBusDate)+"</BusDate>\r\n");
		for(int rIdx=0; rIdx < pRpDataList.size(); rIdx++){
			ReceiptData aData = pRpDataList.get(rIdx);
			sbXml.append(SerializeHelper.toXmlItemByAttr(aData, ignoreSet));
			sbXml.append("\r\n");
		}
		sbXml.append("</TransParam>\r\n</TransReq></ATSYH>");
		return sbXml;
	}
	
	/**组装成json格式*/
	private StringBuilder buildJosnSb2(String pTenant,ArrayList<ReceiptData> pRpDataList) throws ParseException,UnsupportedEncodingException{
		StringBuilder sendJson = new StringBuilder();
		sendJson.append("tenantId=" + pTenant + "&transType=downloadreceipt&data=");
		sendJson.append("{\"rows\":[");
		for(int rIdx=0;rIdx<pRpDataList.size();rIdx++){
			ReceiptData aData = pRpDataList.get(rIdx);
			sendJson.append("{");
			sendJson.append("\"actNum\":\"" + aData.ownAct + "\",");
			sendJson.append("\"actCur\":\"" + aData.cur + "\",");
			sendJson.append("\"receiptCode\":\"" + aData.receiptNo + "\",");// 打印示例号，目前招行特有，其它银行同 回单编号
			sendJson.append("\"receiptType\":\"" + aData.receiptType + "\",");// 回单类型 1：存款回单；2：取款回单；3：转账回单；4：缴税回单；5：收费回单；6定期回单；7：活期回单
			sendJson.append("\"receiptState\":\"" + "" + "\",");// 回单状态 1：正常；2：冲正；3：被冲正；4：当日冲正
			sendJson.append("\"oppActName\":\"" + aData.oppActName + "\",");
			sendJson.append("\"oppActNum\":\"" + aData.oppActNum + "\",");
            String amount = "";
            if(aData.amount!=null){
            	amount = Format.DecimalAmtFormat.format(aData.amount);
            }
			sendJson.append("\"amount\":\"" + amount + "\",");
			sendJson.append("\"moneyway\":\"" + aData.moneyWay + "\",");
			sendJson.append("\"tranSeqNo\":\"" + aData.receiptNo + "\",");
			sendJson.append("\"bnkSeqNo\":\"" + aData.bnkSeqNo + "\",");
			sendJson.append("\"tranCode\":\"" + aData.tranCode + "\",");// 交易码?
			sendJson.append("\"tranDate\":\"" + Format.Date10Format.format(aData.getBusDate()) + "\",");
            String filePath = aData.upFile;
            if(StringHelper.isNullOrEmpty(filePath)){
            	filePath = aData.localFile;
            }
			sendJson.append("\"filePath\":\"" + filePath + "\",");
			sendJson.append("\"fileType\":\"" + getFileType(filePath) + "\"");
			sendJson.append("}");
          //判断不是最后一笔加","，最后一笔不需要加
            if(rIdx < (pRpDataList.size()-1) ){
				sendJson.append(",");
            }
		}
		sendJson.append("]}");
		return sendJson;
	}

	private StringBuilder buildJosnSb(String pTenant,ArrayList<ReceiptData> pRpDataList) throws ParseException,UnsupportedEncodingException{
		StringBuilder sendJson = new StringBuilder();
		StringBuilder dataJson = new StringBuilder();
		sendJson.append("tenantId=" + pTenant + "&transType=downloadreceipt&data=");
		dataJson.append("{\"rows\":[");
		for(int rIdx=0;rIdx<pRpDataList.size();rIdx++){
			ReceiptData aData = pRpDataList.get(rIdx);
			dataJson.append("{");
			dataJson.append("\"actNum\":\"" + aData.ownAct + "\",");
			dataJson.append("\"actCur\":\"" + aData.cur + "\",");
			dataJson.append("\"receiptCode\":\"" + aData.receiptNo + "\",");// 打印示例号，目前招行特有，其它银行同 回单编号
			dataJson.append("\"receiptType\":\"" + aData.receiptType + "\",");// 回单类型 1：存款回单；2：取款回单；3：转账回单；4：缴税回单；5：收费回单；6定期回单；7：活期回单
			dataJson.append("\"receiptState\":\"" + "" + "\",");// 回单状态 1：正常；2：冲正；3：被冲正；4：当日冲正
			dataJson.append("\"oppActName\":\"" + aData.oppActName + "\",");
			dataJson.append("\"oppActNum\":\"" + aData.oppActNum + "\",");
			String amount = "";
			if(aData.amount!=null){
				amount = Format.DecimalAmtFormat.format(aData.amount);
			}
			dataJson.append("\"amount\":\"" + amount + "\",");
			dataJson.append("\"moneyway\":\"" + aData.moneyWay + "\",");
//			dataJson.append("\"tranSeqNo\":\"" + aData.receiptNo + "\",");
            dataJson.append("\"tranSeqNo\":\"" + aData.busRef + "\",");
			dataJson.append("\"bnkSeqNo\":\"" + aData.bnkSeqNo + "\",");
			dataJson.append("\"tranCode\":\"" + aData.tranCode + "\",");// 交易码?
			dataJson.append("\"tranDate\":\"" + Format.Date10Format.format(aData.getBusDate()) + "\",");
			String filePath = aData.upFile;
			if(StringHelper.isNullOrEmpty(filePath)){
				filePath = aData.localFile;
			}
			dataJson.append("\"filePath\":\"" + filePath + "\",");
			dataJson.append("\"fileType\":\"" + getFileType(filePath) + "\"");
			dataJson.append("}");
			//判断不是最后一笔加","，最后一笔不需要加
			if(rIdx < (pRpDataList.size()-1) ){
				dataJson.append(",");
			}
		}
		dataJson.append("]}");
		sendJson.append(URLEncoder.encode(dataJson.toString(),"GBK"));
		return sendJson;
	}
	
	private String[] pushToNbGateway(String pServerUrl, StringBuilder pJosn){
		StringBuilder pushLog = new StringBuilder();
		String[] data= FormSubmit.receiptInfoSend(pServerUrl, pJosn.toString(), pushLog);

        FGBiz.logTrace.debug(logCode,logID + "\t"+pushLog.toString());

        return data;
	}
	
	//上送格式为json格式
	private  String[] sendToATS(String pPushUrl, String jsonData, String pCharset, int pTimeOut) throws Exception{
		String[] retStr = new String[]{"",""};
		
		//发送ATS
		WebRequest tmpRequest = new WebRequest(pPushUrl);
		tmpRequest.setConnectTimeout(10000);
		tmpRequest.setReadTimeout(pTimeOut);
		tmpRequest.setRequestProperty("connection", "Close");
		tmpRequest.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
		byte[] tmpWriteBytes = jsonData.getBytes(pCharset);
		retStr = tmpRequest.upload(tmpWriteBytes, pCharset);
		FGBiz.logTrace.debug(logCode, logID + "\t4.返回的数据"+retStr[1]);

		return retStr;
	}
	
	/**推送到远程DSP*/
	private String[] pushToRemoteDsp(DspClient pDspClient, StringBuilder pXml){
		String[] data = new String[]{"", ""};
        try {
			TcpReceiver retReceiver = pDspClient.saveToDb(pXml.toString());
			if(retReceiver.isCompleted){
				String retXml = new String(retReceiver.recBytes, retReceiver.contentStartIndex,retReceiver.contentLength, retReceiver.charsetName);
				FGBiz.logTrace.debug(logCode,logID + "\t响应的报文：" + retXml);
				if(retXml.contains("<") && retXml.contains(">")){
					XmlTextReader retXmlRd = new XmlTextReader(retXml);
					if("0".equals(retXmlRd.getSimpleText("RespCode"))){
						data[1] = "ok";
					}else{
						data[0] = "-1";
						data[1] = retXmlRd.getSimpleText("RespInfo");
						FGBiz.limitMsg.addNoConsole(logCode, retXml);
					}
				}else{
					data[0] = "-1";
					data[1] = retXml;
				}
			}
			else{
				data[0] = retReceiver.connState;
				data[1] = retReceiver.getConnMsg();
			}
		} catch (Exception e) {
			data[0] = "exception";
			data[1] =LogHelper.getStackTrace(e);
		}
        return data;
	}
	
	//推送至atsInsu
	private  String[] pushToAtsInsu(DataPushSet pPushSet, ArrayList<ReceiptData> pDataList) throws Exception{
		String[] retStr = new String[]{"",""};
		StringBuilder sendJson = new StringBuilder();
		sendJson.append("{\"transType\":\"notify\",\"topic\":\"receipt\",\"dataList\":[");
		for(int rIdx=0;rIdx<pDataList.size();rIdx++){
			ReceiptData aData = pDataList.get(rIdx);
			sendJson.append("{");
			sendJson.append("\"receiptNo\":\"" + aData.receiptNo + "\",");// 回单编号
			sendJson.append("\"bnkSeqNo\":\"" + aData.bnkSeqNo + "\",");//银行流水号
			sendJson.append("\"ownAct\":\"" + aData.ownAct + "\",");
			sendJson.append("\"bank\":\"" + aData.bank + "\",");
            sendJson.append("\"busRef\":\"" + aData.busRef + "\",");
            sendJson.append("\"filePath\":\"" + aData.upFile + "\",");
            sendJson.append("\"busDate\":\"" + aData.busDate + "\"");
            sendJson.append("}");
          //判断不是最后一笔加","，最后一笔不需要加
            if(rIdx < (pDataList.size()-1) ){
            	sendJson.append(",");
            }
		}
		sendJson.append("]}");
		String charset = pPushSet.getCharset("utf-8");
		WebRequest webreq = new WebRequest(pPushSet.pushServerUrl);
		webreq.setConnectTimeout(10000);
		webreq.setReadTimeout(pPushSet.getReadTimeout(60000));
		webreq.setRequestProperty("connection", "Close");
		webreq.setRequestProperty("content-type","application/x-www-form-urlencoded");
		FGBiz.logTrace.debug(logCode,logID + "\t发送的报文："+sendJson.toString());
		String reqStr2 = "reqstr="+URLEncoder.encode(sendJson.toString(), charset);

		byte[] upBytes = reqStr2.getBytes(charset);
		
		String[] retUpVals = webreq.upload(upBytes, charset);
		if(retUpVals[0].length()==0){
			FGBiz.logTrace.info(logCode, "响应json：\r\n"+retUpVals[1]);
			HashMap<String, String> hashParam = JSONUtils.json2Map(retUpVals[1]);
			if(hashParam.containsKey("respCode") && "0".equals(hashParam.get("respCode"))){
				retStr[1] = "ok";
			}else{
				String respInfo = "";
				if(hashParam.containsKey("respInfo")){
					respInfo = hashParam.get("respInfo");
				}
				retStr[1] = "返回失败："+respInfo;
			}
		}else{
			retStr[0] = retUpVals[0];
			retStr[1] = "提交"+pPushSet.pushServerUrl+"失败："+retUpVals[0]+"-"+retUpVals[1];
		}
		return retStr;
	}
	
	private static TransIDHelper idHelper = new TransIDHelper(1,999999);
	
	private String getTransSeqId(){
		return Format.DateTime14Format.format(new Date())+idHelper.getNextIDStr();
	}
	
	/**发送至ats进行数据校验*/
	private String[] sendNotifyToAtsCheck(DataPushSet pPushSet, ArrayList<ReceiptData> pDataList) throws Exception{
		String[] retStr = new String[]{"",""};
		StringBuilder sendJson = new StringBuilder();
		sendJson.append("{\"transType\":\"receiptCheck\"");
		sendJson.append(",\"transSeqID\":\""+getTransSeqId()+"\"");
		sendJson.append(",\"callbackUrl\":\""+pPushSet.acceptCallbackUrl+"\"");
		sendJson.append(",\"dataList\":[");
		for(int rIdx=0;rIdx<pDataList.size();rIdx++){
			ReceiptData aData = pDataList.get(rIdx);
			sendJson.append("{");
			sendJson.append("\"receiptNo\":\"" + aData.receiptNo + "\",");// 回单编号
			sendJson.append("\"bnkSeqNo\":\"" + aData.bnkSeqNo + "\",");//银行流水号
			sendJson.append("\"ownAct\":\"" + aData.ownAct + "\",");
			sendJson.append("\"bank\":\"" + aData.bank + "\",");
            sendJson.append("\"busRef\":\"" + aData.busRef + "\",");
            String eUrid = aData.busDate+"_"+ReceiptDataHelper.getTableIdxByAct(aData.ownAct)+"_"+aData.urid;
            sendJson.append("\"urid\":\"" + eUrid + "\",");
            sendJson.append("\"busDate\":\"" + aData.busDate + "\",");
            sendJson.append("\"filePath\":\"" + aData.upFile + "\"");
            sendJson.append("}");
          //判断不是最后一笔加","，最后一笔不需要加
            if(rIdx < (pDataList.size()-1) ){
            	sendJson.append(",");
            }
		}
		sendJson.append("]}");
		String charset = pPushSet.getCharset("utf-8");
		WebRequest webreq = new WebRequest(pPushSet.pushServerUrl);
		webreq.setConnectTimeout(3500);
		webreq.setReadTimeout(pPushSet.getReadTimeout(5000));
		webreq.setRequestProperty("connection", "Close");
		webreq.setRequestProperty("content-type","application/json");
		String sendReqStr = sendJson.toString();
		FGBiz.logTrace.debug(logCode,logID + "\t发送的报文："+sendReqStr);
		byte[] upBytes = sendReqStr.getBytes(charset);
		
		String[] retUpVals = webreq.uploadNoNeedResp(upBytes);//, "utf-8"
		if(retUpVals[0].length()==0){
			retStr[1] = "ing";
		}else{
			retStr[0] = retUpVals[0];
			retStr[1] = "提交"+pPushSet.pushServerUrl+"失败："+retUpVals[0]+"-"+retUpVals[1];
		}
		return retStr;
	}
}
